package com.shenyue.nbtownlet.activity;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.base.BaseActivity;
import com.shenyue.nbtownlet.databinding.ActivityLoginBinding;
import com.shenyue.nbtownlet.event.BaseListener;
import com.shenyue.nbtownlet.model.AppLoginModel;
import com.shenyue.nbtownlet.util.ActivityUtil;
import com.shenyue.nbtownlet.util.NetWorkUtils;
import com.shenyue.nbtownlet.util.StorageUtil;
import com.shenyue.nbtownlet.util.StringUtil;
import com.trycatch.mysnackbar.Prompt;
import com.trycatch.mysnackbar.TSnackbar;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private ActivityLoginBinding binding;
    private Handler mHandler = new Handler();
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        setActionBar(true, "用户登录");

        binding.btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn: {
                if (!NetWorkUtils.isExistNetwork(this))
                    return;
                String username = binding.username.getText().toString().trim();
                String password = binding.password.getText().toString().trim();
                if (StringUtil.isNull(username)) {
                    showMsg("请输入用户名");
                    binding.username.requestFocus();
                    return;
                }
                if (StringUtil.isNull(password)) {
                    showMsg("请输入密码");
                    binding.password.requestFocus();
                    return;
                }
                final TSnackbar snackBar = showTSnackbar("正在登陆, 请稍候...");
                JSONObject dec = ActivityUtil.getDevice(mContext);
                JSONObject param = new JSONObject();
                param.put("Account", username);
                param.put("Pwd", password);
                param.put("IMEI", StringUtil.getValue(dec.getString("IMEI")));
                param.put("Name", StringUtil.getValue(dec.getString("PhoneName")));
                param.put("Type", StringUtil.getValue(dec.getString("PhoneType")));
                AppLoginModel.login(mContext, param, new BaseListener() {
                    @Override
                    public void success(final String val, final String action) {
                        try {
                            StorageUtil.setLocalStorage(mContext, AppLoginModel.LOGIN_NATIVE_DATA, val);
                            hideTSnackbar(snackBar, "登录成功", Prompt.SUCCESS);
                            mHandler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    finish();
                                }
                            }, 500);
                        } catch (Exception e) {
                            hideTSnackbar(snackBar, val, Prompt.WARNING);
                        }
                    }

                    @Override
                    public void error(String error, String action) {
                        hideTSnackbar(snackBar, error, Prompt.WARNING);
                    }
                });
            }
            break;
        }
    }
}
